package com.cloud.punish.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cloud.punish.api.bean.SearchCondition;

public class SqlConditionParserHelper {
	private static final Logger logger = LoggerFactory.getLogger(SqlConditionParserHelper.class);

	private static final String COMPARTOR_LIKE = "like";
	private static final String COMPATOR_BETWEEN = "between";

	/**
	 * 通过动态条件字段解析sql
	 * 
	 * @param conditions
	 * @return
	 */
	public static String buildConditionSql(List<SearchCondition> conditions) {
		StringBuilder sqlBuilder = new StringBuilder();
		for (SearchCondition cond : conditions) {
			List<Object> values = cond.getValue();
			if (null != values && values.size() > 0) {
				sqlBuilder.append(" and ").append(cond.getFieldName());
				String comparetor = (String) cond.getComparetor().get("value");
				if ("datetime".equals(cond.getType())) {
					String value1 = "", value2 = "";
					try {
						value1 = (String) values.get(0);
						value1 = value1.replace("Z", " UTC");
						SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
						Date searchDate = format.parse(value1);
						value1 = DateHelper.strToDateSql(DateHelper.dateTimeToStr(DateHelper.getDateBegin(searchDate)),
								5, DateHelper.DB_TYPE_MYSQL);
						if (values.size() == 2) {
							value2 = (String) values.get(1);
							value2 = value2.replace("Z", " UTC");
							SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
							Date searchDate2 = format2.parse(value2);
							value2 = DateHelper.strToDateSql(
									DateHelper.dateTimeToStr(DateHelper.getDateBegin(searchDate2)), 5,
									DateHelper.DB_TYPE_MYSQL);
						} else {
							value2 = DateHelper.strToDateSql(
									DateHelper.dateTimeToStr(DateHelper.getDateEnd(searchDate)), 5,
									DateHelper.DB_TYPE_MYSQL);
						}
						if (COMPATOR_BETWEEN.equals(comparetor) || "=".equals(comparetor)) {
							sqlBuilder.append(" between ").append(value1).append(" and ")
									.append(value2);
						} else if (">=".equals(comparetor)) {
							sqlBuilder.append(" ").append(comparetor).append(" ").append(value1);
						} else if ("<=".equals(comparetor)) {
							sqlBuilder.append(" ").append(comparetor).append(" ").append(value2);
						}
					} catch (ParseException e) {
						logger.error("Unrecognized Date String: {}", value1);
					}
				} else if ("dropdown".equals(cond.getType())) {
					String value = (String) ((Map) values.get(0)).get("value");
					sqlBuilder.append(" ").append(comparetor).append(" '").append(value).append("'");
				} else {
					String value1 = (String) values.get(0), value2 = "99999999999";
					if (values.size() == 2) {
						value2 = (String) values.get(1);
					}
					if (COMPARTOR_LIKE.equals(comparetor)) {
						sqlBuilder.append(" ").append(comparetor).append(" '%").append(value1).append("%'");
					} else if (COMPATOR_BETWEEN.equals(comparetor)) {
						sqlBuilder.append(" ").append(comparetor).append(" ").append(value1).append(" and ")
								.append(value2);
					} else {
						sqlBuilder.append(" ").append(comparetor).append(" '").append(value1).append("'");
					}
				}
			}
		}
		return sqlBuilder.toString();
	}
}
